$(function () {
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    //获取验证码
    var token = localStorage.token;
    var flagL = false;
    $(".counter").on("click",function() {
        if(flagL) return false;
        var mobile = $(".newPhone").val();
        if(!mobile) {
            tips("请输入手机号码");
            return false;
        }
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))){ 
            tips("请输入正确的手机号码");
            return false; 
        }
        if(mobile===localStorage.userMobile) {
            tips("请输入新的手机号码");
            return false;
        }
        $.ajax({
            type: "post",
            url: domain.test + "/gettelcode",
            data: {
                mobile:mobile
            },
            success: function (response) {
                console.log(response);
                if(response.data.rtnCode===200) {
                    var counter = 90;
                    var timer = setInterval(function() {
                        counter--;
                        if(counter===0) {
                            $(".counter").html("获取短息验证码");
                            clearInterval(timer);
                        }else {
                            $(".counter").html(counter + "s");
                            flagL = true;
                        }
                    },1000);
                }
            }
        });
    });
    $(".submitBtn").on("click",function() {
        var userName = $(".userName").val();
        if(!userName) {
            tips("请输入用户账号");
            return false;
        }
        var password = $(".password").val();
        if(!password) {
            tips("请输入密码");
            return false;
        }
        var mobile = $(".newPhone").val();
        if(!mobile) {
            tips("请输入手机号码");
            return false;
        }
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))){ 
            tips("请输入正确的手机号码");
            return false; 
        }
        var captcha = parseInt($(".code").val());
        if(!captcha) {
            tips("请输入验证码");
            return false;
        }
        $.ajax({
            type: "post",
            url: domain.test + "/bindnewphone",
            data: {
                mobile:mobile,
                captcha:captcha,
                userName:userName,
                password:password
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                    tips("修改成功");
                    localStorage.userMobile = mobile;
                    location.href = "./myMsg.html";
                }
            }
        });
     });
});